package com.best.onegous.Refresh;





import com.best.onegous.R;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.MeasureSpec;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class XListViewHeader extends LinearLayout {
	private LinearLayout mContainer;
	private int mState = STATE_NORMAL;

	
	public final static int STATE_NORMAL = 0;
	public final static int STATE_READY = 1;
	public final static int STATE_REFRESHING = 2;
	public final static int M = 3;
	
	private MeiTuanRefreshFirstStepView mFirstView;
	private MeiTuanRefreshSecondStepView mSecondView;
	private AnimationDrawable secondAnim;
	private MeiTuanRefreshThirdStepView mThirdView;
	private AnimationDrawable thirdAnim;
	private LinearLayout.LayoutParams lp;
	private int h;
	public XListViewHeader(Context context) {
		super(context);
		initView(context);
	}

	/**
	 * @param context
	 * @param attrs
	 */
	public XListViewHeader(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
				LayoutParams.MATCH_PARENT, 0);
		mContainer = (LinearLayout) LayoutInflater.from(context).inflate(
				R.layout.xlistview_header, null);
		measureView(mContainer);
		h  = mContainer.getMeasuredHeight();
		addView(mContainer, lp);
		setGravity(Gravity.BOTTOM);
		
		
		
		mFirstView = (MeiTuanRefreshFirstStepView) mContainer.findViewById(R.id.first_view);
		//tv_pull_to_refresh = (TextView) headerView.findViewById(R.id.tv_pull_to_refresh);
		mSecondView = (MeiTuanRefreshSecondStepView) mContainer.findViewById(R.id.second_view);
		mSecondView.setBackgroundResource(R.drawable.pull_to_refresh_second_anim);
		secondAnim = (AnimationDrawable) mSecondView.getBackground();
		mThirdView = (MeiTuanRefreshThirdStepView) mContainer.findViewById(R.id.third_view);
		mThirdView.setBackgroundResource(R.drawable.pull_to_refresh_third_anim);
		thirdAnim = (AnimationDrawable) mThirdView.getBackground();
		
		
	}

	public void setState(int state,float delta) {
		//Log.i("dddddddddddd", delta+"--");
		mState = state;
		
/*		
		if (state == STATE_REFRESHING) {	//���ڼ���
			mArrowImageView.clearAnimation();
			mArrowImageView.setVisibility(View.INVISIBLE);
			mProgressBar.setVisibility(View.VISIBLE);
		} else {	// ��ʾ��ͷͼƬ                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
			mArrowImageView.setVisibility(View.VISIBLE);
			mProgressBar.setVisibility(View.INVISIBLE);
			
		}*/
		
		switch(state){
		case M://ˢ�����
			thirdAnim.stop();
			
			break;
		case STATE_NORMAL://����ˢ��
		/*	if (mState == STATE_READY) {
				mArrowImageView.startAnimation(mRotateDownAnim);
			}
			if (mState == STATE_REFRESHING) {
				mArrowImageView.clearAnimation();
			}*/
			
			mFirstView.setVisibility(View.VISIBLE);
			mSecondView.setVisibility(View.GONE);
			mThirdView.setVisibility(View.GONE);
			mFirstView.setCurrentProgress(delta);
			//�ػ�
			mFirstView.postInvalidate();
			
			
			break;
		case STATE_READY://�ɿ�ˢ��
		/*	if (mState != STATE_READY) {
				mArrowImageView.clearAnimation();
				mArrowImageView.startAnimation(mRotateUpAnim);
				mHintTextView.setText(R.string.xlistview_header_hint_ready);
			}
			*/
			mFirstView.setVisibility(View.GONE);
			//�ڶ�״̬view��ʾ����
			mSecondView.setVisibility(View.VISIBLE);
			//���ŵڶ�״̬�Ķ���
			secondAnim.start();
			//����״̬view��������
			mThirdView.setVisibility(View.GONE);
			//ֹͣ����״̬�Ķ���
			thirdAnim.stop();
			break;
		case STATE_REFRESHING://��ָ̧��   ���ڼ���
			
			
			mFirstView.setVisibility(View.GONE);
			//����״̬view��ʾ����
			mThirdView.setVisibility(View.VISIBLE);
			//�ڶ�״̬view��������
			mSecondView.setVisibility(View.GONE);
			//ֹͣ�ڶ�״̬����
			secondAnim.stop();
			//��������״̬view
			thirdAnim.start();
			break;
		}
		
		mState = state;
	}
	
	public void setVisiableHeight(int height) {
		
			
				
			
			//if(mState!=STATE_REFRESHING){
				if (height < 0){
					height = 0;
				}
				lp = (LinearLayout.LayoutParams) mContainer
						.getLayoutParams();
				lp.height = height;
				mContainer.setLayoutParams(lp);
			/*}else{
				lp = (LinearLayout.LayoutParams) mContainer
						.getLayoutParams();
				lp.height = h;
				mContainer.setLayoutParams(lp);
			}*/
			
		
		
	}

	public int getVisiableHeight() {
		return mContainer.getHeight();
	}
	/**
	 * �򸸲���������С
	 * @param child
	 */
	private void measureView(View child) {
		ViewGroup.LayoutParams p = child.getLayoutParams();
		if (p == null) {
			p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.WRAP_CONTENT);
		}
		int childWidthSpec = ViewGroup.getChildMeasureSpec(0,0, p.width);
		int lpHeight = p.height;
		int childHeightSpec;
		if (lpHeight > 0) {
			childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight,
					MeasureSpec.EXACTLY);
		} else {
			childHeightSpec = MeasureSpec.makeMeasureSpec(0,
					MeasureSpec.UNSPECIFIED);
		}
		child.measure(childWidthSpec, childHeightSpec);
	}

}
